//
//  DetailViewController.m
//  盘盈贷1.2
//
//  Created by iOS on 15/3/6.
//  Copyright (c) 2015年 iOS. All rights reserved.
//

#import "DetailViewController.h"
#import "TopTableViewCell.h"
#import "InformationTableViewCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "Top.h"
#import "CompanyTableViewCell.h"
#import "JiekuanmiaosuTableViewCell.h"
#import "LastTableViewCell.h"
#import "CalculateViewController.h"
#import "TouziViewController.h"
#import "FootTableViewCell.h"
#import "Company.h"
#import "APIRequest.h"
typedef void(^bl) (NSString *userId);
@interface DetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSDictionary *dic;
@property (nonatomic, retain)NSString *api;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)NSMutableArray *ar;


@end

@implementation DetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"详细信息";
     
        self.array = [NSMutableArray array];
        self.api = @"http://192.168.1.221";
        self.arr = [NSMutableArray array];
        self.ar = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
 
    NSLog(@"++++++++%@", self.userId);

    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-50) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    //self.tableView.rowHeight = 200;
    self.tableView.scrollEnabled = YES;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    [self LoadingData];
    
    //计算器按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(self.view.frame.size.width / 4.1, self.view.frame.size.height / 1.17, 120, 30);
    button.center = CGPointMake(self.view.frame.size.width / 4.1, self.view.frame.size.height / 1.17);
    [button setTitle:@"计算器" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor redColor];
    button.tintColor = [UIColor whiteColor];
    [button addTarget:self action:@selector(jisuanqi:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    //投资按钮
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(self.view.frame.size.width / 1.3, self.view.frame.size.height / 1.17, 120, 30);
    button1.center = CGPointMake(self.view.frame.size.width / 1.3, self.view.frame.size.height / 1.17);
    [button1 setTitle:@"投资" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor redColor];
    button1.tintColor = [UIColor whiteColor];
    [button1 addTarget:self action:@selector(touzi:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];


}
- (void)LoadingData
{
    NSString *str = [NSString stringWithFormat:@"http://www.cloudpeng.com:8102/product/detail?id=%@", self.userId];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.dic = responseObject;
       // NSLog(@"~~~%@", self.dic);
        self.arr = [self.dic objectForKey:@"Bids"];
        self.ar = [[[self.dic objectForKey:@"DBProduct"] objectForKey:@"ProductCate"] objectForKey:@"Product"];
        [self.tableView reloadData];
        
       // NSLog(@"arr:%@", [[self.dic objectForKey:@"DBProduct"] objectForKey:@"Id"] );
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"~~~~~~~%@",error);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 || section == 1 || section == 2 || section == 3 || section == 4 ) {
        return 1;
    } else if (section == 9){
        return [self.arr count];
    }else{
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0){
        return 30;
    } else if (indexPath.section == 1) {
        return 180;
    } else if (indexPath.section == 2) {
        return 30;
    }else if (indexPath.section == 3){
        return 200;
    } else if (indexPath.section == 4){
        return 30
        ;
    }else if (indexPath.section == 5){
        return 30;
    }else if (indexPath.section == 6){
        return 30;
        
    }else if (indexPath.section == 7){
        return 200;
    } else if (indexPath.section == 8){
        return 30;
    }else{
        return 30;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"11"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"11"];
            
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.text = @"担保借款-周转资金";
        
        return cell;
        
    } else if (indexPath.section == 1) {
        
        TopTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"33"];
        if (cell1 == nil) {
            cell1 = [[TopTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"33"];
            
        }
        
        [cell1 setSelectionStyle:UITableViewCellSelectionStyleGray];
         if ([[[self.dic objectForKey:@"DBProduct"] objectForKey:@"Amount"] intValue] > 100000) {
             NSMutableString *t2 = [NSMutableString stringWithFormat:@"融资金额:%@.00", [[self.dic objectForKey:@"DBProduct"] objectForKey:@"Amount"]];
             [t2 insertString:@"," atIndex:6];
        [t2 insertString:@"," atIndex:10];
             cell1.jine.text = t2;}else{
        NSMutableString *t1 = [NSMutableString stringWithFormat:@"融资金额:%@.00", [[self.dic objectForKey:@"DBProduct"] objectForKey:@"Amount"]];
        [t1 insertString:@"," atIndex:7];
        
                 cell1.jine.text = t1;}
//        cell1.jine.text = [NSString stringWithFormat:@"融资金额:%@", [[self.dic objectForKey:@"DBProduct"] objectForKey:@"Amount"]];
        //cell1.zhuanyebaozheng.text = [NSString stringWithFormat:@"专业保证:%@", [[self.dic objectForKey:@"DBProduct"] objectForKey:@"Guarantee"]];
        
        
       
        cell1.zhuanyebaozheng.text = [NSString stringWithFormat:@"专业保证:%@", [[self.dic  objectForKey:@"DBProduct"] objectForKey:@"Guarantee"]];
        NSLog(@"%@", [[self.ar firstObject] objectForKey:@"Guarantee"]);
        NSString *load = [NSString stringWithFormat:@"年化收益:%@",[[self.dic objectForKey:@"DBProduct"] objectForKey:@"LoanRate"]];
        if ([load rangeOfString:@"."].length>0) {
            
            cell1.nianhuashouyi.text = [NSString stringWithFormat:@"%@0%%", load];
        }else{
            cell1.nianhuashouyi.text = [NSString stringWithFormat:@"%@ .00%%", load];
        }
//        cell1.nianhuashouyi.text = [NSString stringWithFormat:@"年化收益:%@.00%%",[[self.dic objectForKey:@"DBProduct"] objectForKey:@"LoanRate"]];
        cell1.rongziqixian.text = [NSString stringWithFormat:@"融资期限:%@个月", [[self.dic objectForKey:@"DBProduct"] objectForKey:@"Limit"]];
        //cell1.zuiditouzi.text = [NSString stringWithFormat:@"最低投资:%@.0", [[self.dic objectForKey:@"DBProduct"] objectForKey:@"MinBid"]];
       
        
        if ([[[[self.dic objectForKey:@"DBProduct"] objectForKey:@"ProductCate"] objectForKey:@"MinBid"] intValue] > 10000) {
            NSMutableString *t3 = [NSMutableString stringWithFormat:@"最低投资:%@.00", [[[self.dic objectForKey:@"DBProduct"] objectForKey:@"ProductCate"] objectForKey:@"MinBid"]];
            [t3 insertString:@"," atIndex:7];
            
            cell1.zuiditouzi.text = t3;}else{
                 cell1.zuiditouzi.text = [NSString stringWithFormat:@"最低投资:%@.00", [[[self.dic objectForKey:@"DBProduct"] objectForKey:@"ProductCate"] objectForKey:@"MinBid"]];
               }
        
      
        if ([[NSString stringWithFormat:@"%@", [[self.dic objectForKey:@"DBProduct"] objectForKey:@"RepayType"]]isEqual:@"1"]) {
            cell1.huankuanfanshi.text = [NSString stringWithFormat:@"还款方式:%@", @"等额本息"];
        }
        if ([[NSString stringWithFormat:@"%@", [[self.dic objectForKey:@"DBProduct"] objectForKey:@"RepayType"]]isEqual:@"2"]) {
            cell1.huankuanfanshi.text = [NSString stringWithFormat:@"还款方式:%@", @"付息还本"];
        }
        if ([[NSString stringWithFormat:@"%@", [[self.dic objectForKey:@"DBProduct"] objectForKey:@"RepayType"]]isEqual:@"3"]) {
            cell1.huankuanfanshi.text = [NSString stringWithFormat:@"还款方式:%@", @"到期还本付息"];
        }
        NSLog(@"repleay:%@",[[self.dic objectForKey:@"DBProduct"] objectForKey:@"RepayType"]);
        NSString *Time = [[self.dic objectForKey:@"DBProduct"] objectForKey:@"BeginTime"];
        NSArray *timearray = [Time componentsSeparatedByString:@"T"]; //从字符A中分隔成2个元素的数组
        NSLog(@"time:%@", [timearray objectAtIndex:1]);
        cell1.shengoushijian.text = [NSString stringWithFormat:@"申购时间:%@", [timearray objectAtIndex:indexPath.row]];
         cell1.shijian.text = [timearray objectAtIndex:1];
        return cell1;
       
        
    }
    
    else if (indexPath.section == 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"33"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"33"];
            
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.text = @"借款人信息";
        return cell;
        
    } else if (indexPath.section == 3) {
        InformationTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"33"];
        if (cell2 == nil) {
            cell2 = [[InformationTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"33"];
            
        }
        if ([[[self.dic objectForKey:@"Details"] objectForKey:@"Sex"]isEqual:@"1"]) {
           cell2.sex.text = [NSString stringWithFormat:@"性别:%@",  @"女"];
        } else {
            cell2.sex.text = [NSString stringWithFormat:@"性别:%@",  @"男"];
        }
        if ([[[self.dic objectForKey:@"Details"] objectForKey:@"Education"]isEqual:@"2"]) {
            cell2.jiaoyuchengdu.text = [NSString stringWithFormat:@"教育程度:%@",  @"本科"];
        } else {
            cell2.jiaoyuchengdu.text = [NSString stringWithFormat:@"教育程度:%@",  @"本科"];
        }
        if ([[[self.dic objectForKey:@"Details"] objectForKey:@"Marry"]isEqual:@"2"]) {
            cell2.hunyin.text = [NSString stringWithFormat:@"婚姻:%@",  @"未婚"];
        } else {
            cell2.hunyin.text = [NSString stringWithFormat:@"婚姻:%@",  @"未婚"];
        }
        [cell2 setSelectionStyle:UITableViewCellSelectionStyleNone];
        NSMutableArray *arr1 = [self.dic objectForKey:@"Loans"];
        cell2.name.text = [NSString stringWithFormat:@"姓名:%@",[[arr1 firstObject] objectForKey:@"RealName"]];

        
        NSString *str =  [[self.dic objectForKey:@"Details"] objectForKey:@"IDCardNumber"];
        if (str.length == 18) {
            NSString *str1 = [str substringWithRange:NSMakeRange(3,12)];
            NSString *str2 = [str stringByReplacingOccurrencesOfString:str1 withString:@"************"];
            cell2.shenfenzheng.text = [NSString stringWithFormat:@"身份证号:%@", str2];
        } else if (str.length == 15){
            NSString *str1 = [str substringWithRange:NSMakeRange(5,9)];
            NSString *str2 = [str stringByReplacingOccurrencesOfString:str1 withString:@"*******"];
            cell2.shenfenzheng.text = [NSString stringWithFormat:@"身份证号:%@", str2];
        }else {
            
        }
       
        
         cell2.hangye.text = @"行业";
        //cell2.gongsiguimo.text = [NSString stringWithFormat:@"公司规模:%@", [[self.dic objectForKey:@"Details"] objectForKey:@"CompanySize"]];
        cell2.gongsiguimo.text = @"公司规模";
        //cell2.gongsizhiwu.text = [NSString stringWithFormat:@"公司职务:%@", [[self.dic objectForKey:@"Details"] objectForKey:@"ZhiWu"]];
        cell2.gongsizhiwu.text = @"公司职务";
        //cell2.companyName.text = [NSString stringWithFormat:@"公司名称:%@", [[self.dic objectForKey:@"Details"] objectForKey:@"CompanyName"]];
        cell2.companyName.text = @"公司名称";
        
        
        return cell2;
    }
    
    
    else if (indexPath.section == 4) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"55"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"55"];
            
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.text = @"借款企业信息";
        return cell;
        
    } else  if (indexPath.section == 5){
        CompanyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"55"];
        if (cell == nil) {
            cell = [[CompanyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"55"];
            
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//        cell.companyName.text = [NSString stringWithFormat:@"公司名称:%@", [[self.dic objectForKey:@"RootProduct"] objectForKey:@"Guarantee"]];
//        NSLog(@"++++++++%@", [[self.dic objectForKey:@"RootProduct"] objectForKey:@"Guarantee"]);
//        Company *model = [self.arr objectAtIndex:indexPath.row];
//        cell.companyName.text = model.Guarantee;
//        cell.companyName.text = [NSString stringWithFormat:@"公司名称:%@",[[self.dic objectForKey:@"RootProduct"] objectForKey:@"Guarantee"]];
        return cell;
        
    }else if (indexPath.section == 6){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"55"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"55"];
            
        }
        cell.textLabel.text = @"借款描述";
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        return cell;
    } else if (indexPath.section == 7){
        JiekuanmiaosuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"55"];
        if (cell == nil) {
            cell = [[JiekuanmiaosuTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"55"];
            
        }
      
        NSMutableArray *arr = [self.dic objectForKey:@"Loans"];
        cell.text.text = [[arr firstObject] objectForKey:@"Description"];
        return cell;
    }else if (indexPath.section == 8){
        LastTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"55"];
        if (cell == nil) {
            cell = [[LastTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"55"];
            
        }
       
      
        return cell;
    }else {
        FootTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"77"];
        if (Cell == nil) {
            Cell = [[FootTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"77"];
        }
       // Cell.xuhao.text = [self.arr objectAtIndex:indexPath.row];
        Cell.touziren.text = [[[self.arr objectAtIndex:indexPath.row] objectForKey:@"User"] objectForKey:@"UserName"];
        Cell.touzijine.text = [NSString stringWithFormat:@"%@",[[self.arr objectAtIndex:indexPath.row] objectForKey:@"Amount"]];
        NSString *a = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"BidTime"];
        //int b = [[a substringToIndex:20] intValue];
        //NSLog(@"+++++++%d", b);
        Cell.time.text = [NSString stringWithFormat:@"%@", a];
        //Cell.time.text = [NSString stringWithFormat:@"%@", [[self.arr objectAtIndex:indexPath.row] objectForKey:@"BidTime"]];
        return Cell;
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (void)jisuanqi:(id)sender
{
    CalculateViewController *cal = [[CalculateViewController alloc] init];
    [self.navigationController pushViewController:cal animated:YES];
    [cal release];
    
}
- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"~~~~~~~~~~~~~~~%@", self.userId);
}
- (void)touzi:(id)sender
{
    TouziViewController *touzi = [[TouziViewController alloc] init];
    [self.navigationController pushViewController:touzi animated:YES];
    [touzi release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
